<?php
	class baivietdb
	{
	   public function Add($MaChuDe = '', $MaNguoiDung = 1, $TieuDe = '', $TomTat = '', $NoiDung = '', $NgayDang = '', $LuotXem = 0, $KiemDuyet = 1)
       {
            $sql = "INSERT INTO tbl_baiviet(MaChuDe, MaNguoiDung, TieuDe, TomTat, NoiDung, NgayDang, LuotXem, KiemDuyet) VALUES('$MaChuDe', $MaNguoiDung, '$TieuDe', '$TomTat', '$NoiDung', '$NgayDang', $LuotXem, $KiemDuyet)";
            
            $db = new dataservice();
			return $db->ExecuteNonQuery($sql);
       }
       
       public function Update($MaBaiViet = 0, $MaChuDe = '', $MaNguoiDung = 1, $TieuDe = '', $TomTat = '', $NoiDung = '', $NgayDang = '', $LuotXem = 0, $KiemDuyet = 0)
       {
            $sql  = "UPDATE tbl_baiviet ";
            $sql .= "SET MaChuDe = (" . dataservice::QuoteSmart($MaChuDe) . ") ,";
            $sql .= "MaNguoiDung = (" . dataservice::QuoteSmart($MaNguoiDung) . "), ";
            $sql .= "TieuDe = (" . dataservice::QuoteSmart($TieuDe) . "), ";
            $sql .= "TomTat = (" . dataservice::QuoteSmart($TomTat) . "), ";
            $sql .= "NoiDung = (" . dataservice::QuoteSmart($NoiDung) . "), ";
            $sql .= "NgayDang = (" . dataservice::QuoteSmart($NgayDang) . "), ";
            $sql .= "LuotXem = (" . dataservice::QuoteSmart($LuotXem) . "), ";
            $sql .= "KiemDuyet = (" . dataservice::QuoteSmart($KiemDuyet) . ") ";
            $sql .= "WHERE (1)";
            if($MaBaiViet > 0)
	           $sql .= " AND MaBaiViet = (" . $MaBaiViet . ")";
            //echo $sql;
            $db = new dataservice();
			return $db->ExecuteNonQuery($sql);
       }
	   
       public function Delete($MaBaiViet = 0)
       {
            $sql  = "DELETE FROM tbl_baiviet ";
            $sql .= "WHERE (1)";
            if($MaBaiViet > 0)
                $sql .= " AND MaBaiViet = (" . $MaBaiViet . ")";
            
            $db = new dataservice();
    		return $db->ExecuteNonQuery($sql);
       }
       
       public function TotalPage($SoBaiViet = 0, $MaChuDe = '')
        {
            $sql  = "SELECT * FROM tbl_baiviet ";
            $sql .= "WHERE (1)";
            
            if($MaChuDe != '')
                $sql .= " AND MaChuDe = " . dataservice::QuoteSmart($MaChuDe);
                
            $data = new dataservice();
            $TongSoDong = $data->ReturnNumRows($sql);
            $TongSoTrang = ceil($TongSoDong / $SoBaiViet);
            return $TongSoTrang;
        }
        
        public function Paging($SoBaiViet = 1, $P = 1,  $MaChuDe = '')
        {
            $X = ($P-1) * $SoBaiViet;
            $sql  = "SELECT * FROM tbl_baiviet ";
            $sql .= "WHERE (1)";
            
            if($MaChuDe != '')
                $sql .= " AND MaChuDe = " . dataservice::QuoteSmart($MaChuDe);
                
            $sql .= " LIMIT $X,$SoBaiViet";
            $data = new dataservice();
            return $data->ExecuteQuery($sql);
        }
        
        public function GetListArticle($MaBaiViet = 0, $MaChuDe = '')
		{
			$sql =	"SELECT BV.MaBaiViet, BV.MaChuDe, BV.MaNguoiDung, BV.TieuDe, BV.TomTat, BV.NoiDung, BV.NgayDang, BV.LuotXem, BV.KiemDuyet, CD.TenChuDe ";
			$sql .=	"FROM tbl_baiviet BV, tbl_chude CD ";
			$sql .=	"WHERE (1) ";
            $sql .= "AND BV.MaChuDe = CD.MaChuDe";
            
            if($MaBaiViet != 0)
                $sql .= " AND BV.MaBaiViet IN (" . $MaBaiViet . ")";
            if($MaChuDe != '')
                $sql .= " AND BV.MaChuDe = " . dataservice::QuoteSmart($MaChuDe);
            
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function IssetArticle($TieuDe = "")
		{
			$sql = "SELECT COUNT(*) AS SoDong FROM tbl_baiviet WHERE (1)";
			
			if($TieuDe != "")
				$sql .= " AND TieuDe = '$TieuDe'";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
        
        public function GetListArticleNotIn($TieuDe = "", $MaChuDe = "")
		{
			$sql = "SELECT * FROM tbl_baiviet WHERE (1)";
			
			if($TieuDe != "")
				$sql .= " AND TieuDe != '$TieuDe'";
            if($MaChuDe != "")
				$sql .= " AND MaChuDe = '$MaChuDe'";
            echo $sql;       
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
	}
?>